#define DEBUG
#include <cstdio>
#include <cstring>

using namespace std;

const int maxh=10, maxn=10, maxm=10, maxk=10, inf=0x7fffffff;

long long min(long long a, long long b) {
  return a<b ? a : b;
}

void spfa(int n, int m, long long dis[maxn+1][maxm+1], int a[maxn+1][maxm+1]) {
  const int fx[4][2] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};
  static int que[maxn*maxm+1][2];
  static bool book[maxn+1][maxm+1];
  for (int i=1; i<=n; i++) {
    for (int j=1; j<=m; j++) {
      que[i*m+j-m][0] = i, que[i*m+j-m][1] = j;
      book[i][j] = true;
    }
  }
  for (int h=0, t=n*m; h!=t; ) {
    h = h%(n*m)+1;
    int x=que[h][0], y=que[h][1];
    book[x][y] = false;
    for (int i=0; i<4; i++) {
      int nx=x+fx[i][0], ny=y+fx[i][1];
      if (nx>0 && ny>0 && nx<=n && ny<=m && dis[nx][ny]>dis[x][y]+a[nx][ny]) {
	dis[nx][ny] = dis[x][y]+a[nx][ny];
	if (!book[nx][ny]) {
	  book[nx][ny] = true;
	  t = t%(n*m)+1;
	  que[t][0] = nx, que[t][1] = ny;
	}
      }
    }
  }
}

int main() {
  freopen("treasure.in", "r", stdin);
  freopen("treasure.out", "w", stdout);

  static int a[maxh+1][maxn+1][maxm+1];
  int h, n, m;
  scanf("%d %d %d", &h, &n, &m);
  for (int i=1; i<=h; i++) {
    for (int j=1; j<=n; j++) {
      for (int k=1; k<=m; k++) {
	scanf("%d", &a[i][j][k]);
      }
    }
  }

  static int tre[maxh+1][maxk+1][2], k[maxh+1];
  for (int i=1; i<=h; i++) {
    scanf("%d", k+i);
    for (int j=1; j<=k[i]; j++) {
      scanf("%d %d", tre[i][j], tre[i][j]+1);
    }
  }

  static long long f[1<<maxk][maxn+1][maxm+1], g[maxn+1][maxm+1];
  for (int hi=1; hi<=h; hi++) {
    memset(f, 60, sizeof(f));
    for (int i=1; i<=n; i++) {
      for (int j=1; j<=m; j++) {
	f[1][i][j] = g[i][j]+a[hi][i][j];
      }
    }
    for (int i=1; i<=k[hi]; i++) {
      int x=tre[hi][i][0], y=tre[hi][i][1];
      f[1<<i][x][y] = a[hi][x][y];
    }

    for (int s=1; s<(1<<(k[hi]+1)); s++) {
      for (int si=s&(s-1); si!=s; si=(si-1)&s) {
	for (int x=1; x<=n; x++) {
	  for (int y=1; y<=m; y++) {
	    f[s][x][y] = min(f[s][x][y], f[si][x][y]+f[s-si][x][y]-a[hi][x][y]);
	  }
	}
      }
      spfa(n, m, f[s], a[hi]);
    }

    for (int i=1; i<=n; i++) {
      for (int j=1; j<=m; j++) {
	g[i][j] = f[(1<<(k[hi]+1))-1][i][j];
      }
    }
  }

  int ans=inf;
  for (int i=1; i<=n; i++) {
    for (int j=1; j<=m; j++) {
      ans = min(ans, g[i][j]);
    }
  }
  printf("%d", ans);

  fclose(stdin);
  fclose(stdout);
  return 0;
}
